<?php
/*

  Copyright (c) 2008   by  maxim.oransky <maxim.oransky@gmail.com>,
  auhtor of  the-wheel-library  team

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

class Dir {

    public static function getRoot() {

        $root = isset($_SERVER['DOCUMENT_ROOT']) ?
            $_SERVER['DOCUMENT_ROOT'] :
            '';

        $root = self::unixSlashes($root);
        $root = self::delEndslash($root);

        return $root;
    }

    public static function makeIfNotExists($path) {
        if (!file_exists($path))  self::make($path);
    }

    public static function make($path) {
        if (!@mkdir($path,0777) && !(file_exists($path) && is_dir($path)))
            throw new Exception("cant create [$path]");
    }

    public static function checkExistsAndReadable($fname) {

        if (!file_exists($fname))
            throw new Exception("[$fname] isn't exists");

        self::checkReadable($fname);
    }

    private static function checkReadable($path) {

        if (!is_dir($path))
            throw new Exception("[$path] isn't directory");

        if (!is_readable($path))
            throw new Exception("[$path] is not readable");
    }

    public static function checkWritable($path) {

        if (!is_dir($path))
            throw new Exception("[$path] isn't directory");

        if (!is_writeable($path))
            throw new Exception("[$path] is not writeable");
    }

    public static function getFilenames($path, $mask = '*') {
        return array_map('basename', self::getFiles($path, $mask));
    }

    public static function getDirnames($path, $mask = '*') {
        return array_map('basename', self::getDirs($path, $mask));
    }

    public static function getFiles($path, $mask = '*') {
        return array_filter(
            glob(self::addEndslash($path).$mask),
            create_function('$file','return !is_dir($file);')
        );
    }

    public static function getDirs($path, $mask = '*') {
        return array_filter(
            glob(self::addEndslash($path).$mask),
            create_function('$file','return is_dir($file);')
        );
    }

    public static function getSize($dirname) {

        $sum = 0;
        foreach (self::recursiveGetDirs($dirname) as $dir) {
            foreach (self::getFiles($dir) as $file) {
                $sum += filesize($file);
            }
        }
        return $sum;
    }

    public static function recursiveGetDirs($path) {

        $path = self::unixSlashes($path);

        $res = array(self::addEndslash($path));
        foreach (self::getDirs($path) as $dir) {
            $res = array_merge($res, self::recursiveGetDirs($dir));
        }
        return $res;
    }

    public static function unixSlashes($path) {
        return str_replace("\\", '/', $path);
    }

    public static function getUrl($path) {
        $path = self::unixSlashes($path);
        return self::addEndslash(File::getUrl($path));
    }

    public static function addEndslash($path) {
        $path = self::unixSlashes($path);
        if (is_array($path)) return array_map(array('self','addEndslash'),$path);
        return rtrim($path, '/') . '/';
    }

    public static function delEndslash($path) {
        $path = self::unixSlashes($path);
        if (is_array($path)) return array_map(array('self','delEndslash'),$path);
        return rtrim($path, '/');
    }
}

?>